using UnityEngine;
using System.Collections;
using pumpkin.display;
using System.Collections.Generic;

public class EffectManagement
{
	
	private string path = "/Resources/SWFFiles/";
	private static EffectManagement instance;
	
	public enum EffectBroadcastPosition
	{
		overlay,
		back,
	}
	
	EffectManagement ()
	{
		
	}
	
	Dictionary<string,GameObject> effectDic = new Dictionary<string, GameObject> ();
	Transform effectParent;
	
	public static EffectManagement GetInstance ()
	{
		if (instance == null) {
			instance = new EffectManagement ();
		}
		
		return instance;
	}
	
	public void PlaySkillEffect (Vector3 ownPos, Vector3 otherPos, string skillName)
	{
		if (effectParent == null) {
			effectParent = GameObject.Find ("SkillParent").transform;
		}
		
		if (effectParent == null) {
			Debug.LogError ("Fight has not started");
		}
		
		string[] effects = SqliteReader.getInstance ().getIniVal ("Skill", "SkillID", skillName, false, new string[]{"Effect0","Effect1","Effect2","Effect3"});
		
		for (int i = 0; i < effects.Length; i++) {
			if (effects [i] != null && effects [i] != "0") {
				string effectId = effects [i];
				SWFPlayer swfPlayer = effectDic [effectId].GetComponent<SWFPlayer> ();
				
				if (swfPlayer.Layer == 0) {
					swfPlayer.Play (ownPos);
				} else {
					swfPlayer.Play (otherPos);
				}
			}
		}
	}
	
	public void AddIntoPlayList (string effectName, GameObject effect)
	{
		if (effectDic.ContainsKey (effectName) == true) {
			return;
		}
		
		effectDic.Add (effectName, effect);
		
		SWFPlayer swfPlayer = effectDic [effectName].GetComponent<SWFPlayer> ();
		if (swfPlayer == null) {
			swfPlayer = effectDic [effectName].AddComponent<SWFPlayer> ();
		}
				
		string[] effectParams = SqliteReader.getInstance ().getIniVal ("SpecialEffect", "EffectID", effectName, false);
				
		//参数设定//
		swfPlayer.FPS = int.Parse (effectParams [1]);
		swfPlayer.Mode = int.Parse (effectParams [2]);
		swfPlayer.life = float.Parse (effectParams [3]);
		swfPlayer.Layer = int.Parse (effectParams [4]);
		swfPlayer.depth = int.Parse (effectParams [5]);
		swfPlayer.delayTime = float.Parse (effectParams [6]);
			
	}
	
	public void AddEffectCallback (string skillName, GameObject[] targetObjs, DamageVo[] damage, GameObject attackInitiator)
	{
		string[] effects = SqliteReader.getInstance ().getIniVal ("Skill", "SkillID", skillName, false, new string[]{"Effect0","Effect1","Effect2","Effect3"});
		
		for (int i = effects.Length - 1; i >= 0; i--) {
			if (effects [i] != null && effects [i] != "0") {
				SWFPlayer swfPlayer = effectDic [effects [i]].GetComponent<SWFPlayer> ();
				swfPlayer.targetObjs = targetObjs;
				swfPlayer.damages = damage;
				swfPlayer.attackInitiator = attackInitiator;
				
				break;
			}
		}
		
		Debug.Log ("AddEffectCallback" + "===>" + targetObjs.Length);
		EncounterToFight.GetInstance ().CallLength (targetObjs.Length);
	}
	
	public void ClearPlayList ()
	{
		effectDic.Clear ();
	}
	
	public void PlayEffect (GameObject effectBroadcastLayer, string effectFileName)
	{
		PlayEffect (effectBroadcastLayer, effectFileName, EffectBroadcastPosition.overlay);
	}
	
	public void PlayEffect (GameObject effectBroadcastLayer, string effectFileName, EffectBroadcastPosition positionType)
	{
		GameObject effectLayer = GameObject.Find ("effectFileName");
		if (effectLayer == null)
			effectLayer = (GameObject)MonoBehaviour.Instantiate (Resources.Load (ResPath.SKILLEFFECTFILE + effectFileName));
		effectLayer.transform.parent = effectBroadcastLayer.transform;
		effectLayer.transform.localScale = Vector3.one;
		effectLayer.transform.position = effectBroadcastLayer.transform.position;
		
		if (positionType == EffectBroadcastPosition.overlay) {
			effectLayer.transform.position -= Vector3.forward;
		} else if (positionType == EffectBroadcastPosition.back) {
			effectLayer.transform.position += Vector3.forward;
		}
		
		SWFPlayer swfPlayer = effectLayer.AddComponent<SWFPlayer> ();
		swfPlayer.Play (effectLayer.transform.position);
		swfPlayer.autoDestroy = true;
	}
	
	public void PlayEffect (Vector3 effectPosition, string effectFileName)
	{
		GameObject effectLayer = GameObject.Find ("effectFileName");
		if (effectLayer == null)
			effectLayer = (GameObject)MonoBehaviour.Instantiate (Resources.Load (ResPath.SKILLEFFECTFILE + effectFileName));
		
		effectLayer.transform.position = effectPosition;
		
		effectLayer.transform.position -= Vector3.forward;
		
		SWFPlayer swfPlayer = effectLayer.GetComponent<SWFPlayer> ();
		if (swfPlayer == null)
			swfPlayer = effectLayer.AddComponent<SWFPlayer> ();
		swfPlayer.Play (effectLayer.transform.position);
	}
	
}
